home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / vbi.arc / VBI.C < prev    next >
C/C++ Source or Header  |  1987-01-01  |  4KB  |  133 lines

  1. /* vbi.c -- vertical blank interrupt handler [Version 1.0  01/01/87]
  2.  *
  3.  * Copyright (C) 1987 by Amgem, Inc.
  4.  *
  5.  * Permission is hereby granted for anyone to make or distribute copies of
  6.  * this program provided the copyright notice and this permission notice are
  7.  * retained.
  8.  * 
  9.  * This software, or software containing all or part of it, may not be sold
  10.  * except with express permission of the authors.
  11.  *
  12.  * Authors:  Bill Dorsey & John Iarocci
  13.  *
  14.  * If you have any questions or comments, the authors may be reached at
  15.  * The Tanj BBS, (301)-251-0675.  Updates and bug fixes may also be obtained
  16.  * through the above service.
  17.  *
  18.  * The code which follows was compiled using the Mark Williams C compiler,
  19.  * but should be portable with little work to other C compilers.  See the
  20.  * associated documentation for notes on how to convert it for use with other
  21.  * C compilers
  22.  */
  23.  
  24. #include <osbind.h>
  25. #include <basepage.h>
  26. #include "vbi.h"
  27.  
  28. int count;        /* count remaining until next scheduling */
  29. int curpid;        /* current process id */
  30. PROC proctab[NPROC];    /* process table */
  31.  
  32. init()                /* initialization */
  33. {
  34.   int i;
  35.   long ssp;
  36.   int schedule();
  37.  
  38.   count=QUANTUM;        /* # of vblanks per scheduling */
  39.   for (i=0; i<NPROC; i++)
  40.     proctab[i].state=FREE;    /* initialize process table */
  41.   ssp=Super(0L);        /* enter supervisor state */
  42.   for (i=0; i<8; i++) {
  43.     if ((*VBLQUEUE)[i] == (int (*)()) 0) {
  44.       (*VBLQUEUE)[i]=schedule;    /* install scheduler in vblank queue */
  45.       Super(ssp);
  46.       return OK;        /* installation successful */
  47.     }
  48.   }
  49.   Super(ssp);
  50.   return SYSERR;        /* installation failed */
  51. }
  52.  
  53. vbiexit()            /* exit code (leave VBI handler) */
  54. {
  55.   long prglen;
  56.  
  57.   prglen=0x100L+BP->p_tlen+BP->p_dlen+BP->p_blen;
  58.   Ptermres(prglen,0);        /* terminate and stay resident */
  59. }
  60.  
  61. remove()            /* removes VBI handler */
  62. {
  63.   int i;
  64.   long ssp;
  65.   int schedule();
  66.  
  67.   ssp=Super(0L);        /* enter supervisor mode */
  68.   for (i=0; i<8; i++)
  69.     if ((*VBLQUEUE)[i] == schedule) {
  70.       (*VBLQUEUE)[i]=(int (*)()) 0;    /* remove scheduler from vblqueue */
  71.       Super(ssp);        /* resume user mode */
  72.       return OK;        /* return success */
  73.     }
  74.   Super(ssp);            /* resume user mode */
  75.   return SYSERR;        /* return failure */
  76. }
  77.  
  78. schedule()            /* process scheduler */
  79. {
  80.   register int i;
  81.   register PROC *pptr;
  82.  
  83.   DISABLE;            /* disable further vb interrupts */
  84.   if (--count == 0) {        /* schedule when count reaches zero */
  85.     count=QUANTUM;        /* re-initialize count */
  86.     pptr=proctab;        /* get pointer to process table */
  87.     for (i=0; i<NPROC; i++,pptr++)
  88.       switch (pptr->state) {
  89.       case READY:        /* if process ready, set curpid to its */
  90.         curpid=i;        /* process id, and then execute it */
  91.         (*pptr->func)();
  92.         break;
  93.       case SLEEP:        /* if process sleeping, decrement its */
  94.         if (--pptr->count == 0) /* count.  If count reaches zero, set */
  95.           pptr->state=READY;    /* process state back to READY */
  96.         break;
  97.       }
  98.   }
  99.   ENABLE;            /* enable vb interrupts */
  100. }
  101.  
  102. create(func)            /* process creation */
  103.   int (*func)();
  104. {
  105.   register int i;
  106.   register PROC *pptr;
  107.  
  108.   pptr=proctab;            /* initialize pointer to process table */
  109.   for (i=0; i<NPROC; i++,pptr++)
  110.     if (pptr->state == FREE) {    /* find free entry in process table */
  111.       pptr->func=func;        /* store pointer to code in table */
  112.       pptr->state=READY;    /* set process state to READY */
  113.       return i;            /* return process id */
  114.     }
  115.   return SYSERR;        /* process table full --> failure */
  116. }
  117.  
  118. delete(pid)            /* process deletion */
  119.   int pid;
  120. {
  121.   if (proctab[pid].state == FREE)
  122.     return SYSERR;        /* if process already free, return failure */
  123.   proctab[pid].state=FREE;    /* set process state to FREE */
  124.   return OK;            /* return success */
  125. }
  126.  
  127. sleep(tsec)            /* process sleep */
  128.   int tsec;
  129. {
  130.   proctab[curpid].state=SLEEP;    /* set process state to SLEEP */
  131.   proctab[curpid].count=tsec*INTERVAL;    /* set count to proper amount */
  132. }
  133.